package com.topvision.myviewtest.camera;

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.topvision.myviewtest.R;
import com.topvision.prlibrary.PlatManager;
import com.topvision.prlibrary.PlatResultEntity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lucy
 * @date 2018-06-27 14:43
 * @description //TODO
 */

public class TestCameraActivity extends AppCompatActivity implements OnPreviewCallback {
    private static final int PERMISSION_CODE = 100;
    @BindView(R.id.cameraview)
    CameraView mCameraView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_camera);
        ButterKnife.bind(this);
        checkPermission();
        PlatManager.getInstance().initPlatEngine(this);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                PlatResultEntity resultEntity = PlatManager.getInstance().platOcr("sdcard/testorc.jpg");
                if (resultEntity != null) {
                    System.out.println("File :" + resultEntity);
                } else {
                    System.out.println("File : resultEntity = null");
                }
            }
        }, 3000);
    }

    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_CODE);
        } else {
            startCamera();
        }
    }

    private void startCamera() {
        mCameraView.setPreviewCallback(this);
        mCameraView.enableCamera(true);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (PERMISSION_CODE == requestCode && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startCamera();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            // 音量下
            mCameraView.setZoom(5, false);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            // 音量上
            mCameraView.setZoom(5, true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (mCameraView != null)
            mCameraView.enableCamera(false);
        PlatManager.getInstance().destroyPlatEngine(this);
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCameraView.pauseCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCameraView.resumeCamera();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Size size = camera.getParameters().getPreviewSize();
        PlatResultEntity resultEntity = PlatManager.getInstance().platOcr(data, size.width, size.height);
        if (resultEntity != null) {
            System.out.println(resultEntity.toString());
        }
    }
}
